/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons;

import org.dctmcommons.impl.PathBuilder;
import org.junit.*;

import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.common.DfException;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

/**
 * Integration tests for {@link PathBuilder}.
 * @author Mark Davidson
 */
public class PathBuilderIT {

	/**
	 * Tests the factory method.
	 */
	@Test
	public void testGetInstance() {
		System.out.println(">>> testFrom");
		assertThat("Factory method from() returned null",
				   PathBuilder.from("/"),
				   is(notNullValue()));
	}

	/**
	 * Tests path building.
	 * @throws DfException The repository exception
	 */
	@Test
	public void testBuild() throws DfException {
		System.out.println(">>> testBuild");
		
		IDfFolder[] folders = null;
		String testFolderPath = null;
		final IDfSession session = DctmCommonsTestUtil.getSession();
		session.beginTrans();
		
		try {
			testFolderPath = DctmCommonsTestUtil.createTestFolderPath();
			
			System.out.println("Got test folder path: " + testFolderPath);
			
			folders = PathBuilder.from(testFolderPath)
								 .add("folder1")
								 .add("folder2")
								 .build(session);
			session.commitTrans();
		} catch (DfException e) {
			session.abortTrans();
		}
		
		assertThat("Folders array is null", folders, is(notNullValue()));
		assertThat("Invalid number of created folders", folders.length, is(2));

		assertThat("Folder1 object is null",
				   folders[0],
				   is(notNullValue()));
		
		assertThat("Folder1 name is incorrect",
				   folders[0].getObjectName(),
				   is("folder1"));
		
		assertThat("Folder1 path is incorrect",
				   folders[0].getFolderPath(0),
				   is(testFolderPath + "/folder1"));
		
		assertThat("Folder2 object is null",
				   folders[1],
				   is(notNullValue()));
		
		assertThat("Folder2 name is incorrect",
				   folders[1].getObjectName(),
				   is("folder2"));
		
		assertThat("Folder2 path is incorrect",
				   folders[1].getFolderPath(0),
				   is(testFolderPath + "/folder1/folder2"));

		DctmCommonsTestUtil.teardown(testFolderPath, true);
		
		assertThat("Failed to remove test folder",
				   session.getFolderByPath(testFolderPath),
				   is(nullValue()));
		
		DctmCommonsTestUtil.releaseSession();
	}
	
	/**
	 * Setup routine.
	 */
	@Before
	public void before() throws DfException {
	}
	
	/**
	 * Teardown routine.
	 */
	@After
	public void after() throws DfException {
	}
}
